﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ATM_Pro.Public;
using System.IO;

namespace ATM_Pro
{
    public partial class UserEnterForm : Form
    {
        public UserEnterForm()
        {
            InitializeComponent();
            this.AcceptButton = btnEnter;
            InitDbSaveFolder();
        }
        

        private void InitDbSaveFolder()
        {
            string dbFullPath = Application.StartupPath;
            string[] files = Directory.GetFiles(dbFullPath);
            bool hasDbFile = false;
            foreach (string temp in files)
                if (temp.EndsWith(".mdb"))
                    hasDbFile = true;
            if (hasDbFile)
            {
                Properties.Settings.Default.AccessConnectiongString = FileHandle.GeneratorDBConnectionString(dbFullPath);
                Properties.Settings.Default.DB_Path = dbFullPath;
            }
        }


        public string UserName { get; set; }
        public string Pwd { get; set; }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            string name = txtUser.Text;
            string pwd = txtPwd.Text;
            UserEnterCheck ckUser = new UserEnterCheck();
            if (ckUser.CheckUser(name, pwd))
            {
                Properties.Settings.Default.CurUserAndPwd = string.Format("{0}|{1}", name, pwd);
                Properties.Settings.Default.Save();
                this.UserName = name;
                this.Pwd = pwd;
                this.DialogResult = DialogResult.OK;
                log4net.ILog log = Log.Log.GetInstance();
                log.Info(string.Format("用户：{0} 成功登陆", name));
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！", "错误", MessageBoxButtons.OK);
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定退出登陆界面吗？",
                                    "退出登陆",
                                     MessageBoxButtons.OKCancel,
                                     MessageBoxIcon.Information) == DialogResult.OK)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                this.Dispose();
            }
        }

        private void UserEnterForm_Load(object sender, EventArgs e)
        {

        }
    }
}
